<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Model_BuyRefund extends Zend_Db_Table{
	
	protected $_name    = "buy_refund";
    protected $_primary = "id";
    
    public function saveData($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->insert('buy_refund', $data);
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function savePayment($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->insert('buy_payment', $data);
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getPayments($supplier_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('p' => 'buy_payment'), array('id', 'total', 'date'))
                    ->order("p.id")
                    ->where('p.supplier_id = ?', $supplier_id, 'INTEGER');
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getPayment($supplier_id = null){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('p' => 'buy_payment'), array('supplier_id', 'SUM(total) AS total'));
        
        if($supplier_id != null){
            $select->where('p.supplier_id = ?', $supplier_id, 'INTEGER');
            $result = $db->fetchRow($select);
        }else{
            $select->group('p.supplier_id');
            $result = $db->fetchAll($select);
        }
        
        return $result;
    }
    
    public function getDatas(){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('r' => 'buy_refund'), array('SUM(total) AS total'))
                    ->joinRight(array('s' => 'suppliers'), "s.id = r.supplier_id", array('name', 'id as supplier_id'))
                    ->group("s.id")
                    ->order("s.name");
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getData($supplier_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('r' => 'buy_refund'), array('id', 'total', 'date'))
                    ->order("r.id DESC")
                    ->where("r.supplier_id = ?", $supplier_id, "INTEGER");
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function deleteData($id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->delete("buy_refund", "id = $id");
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
}